package com.bbh.plugin.single.privilege.demo.controller;

import com.bbh.common.base.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.boot.config.shiro.base.entity.Principal;


/**
 * Created by roman_000 on 2016/6/24.
 */
public abstract class BasicController extends BaseController {
    protected static final String OPERATE_TYPE = "operateType";
    protected static final String DATA_STATUS = "dataStatus";
    protected static final String VIEW_FILED = "view";
    protected static final String NORMAL_FILED = "normal";
    protected static final String UPDATE_FILED = "uc";
    protected static final String DELETE_FILED = "dh";
    protected static final String INSERT_FILED = "sk";

    /**
     * 获取登录用户信息
     *
     * @return
     */
    public Principal getPrincipal() {
        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();
        return principal;
    }

    /**
     * 获取当前登录用户名
     *
     * @return
     */
    public String getLoginName() {
        String loginName = StringUtils.EMPTY;
        Principal principal = getPrincipal();
        if (principal != null) {
            loginName = principal.getLoginName();
        }
        return loginName;
    }


    /**
     * 获取当前登录用户id
     *
     * @return
     */
    public String getLoginId() {
        String id = StringUtils.EMPTY;
        Principal principal = getPrincipal();
        if (principal != null) {
            id = principal.getId();
        }
        return id;
    }

    /**
     * 是否进行了认证
     *
     * @return
     */
    public boolean isAuthenticated() {
        Subject subject = SecurityUtils
                .getSubject();
        return subject.isAuthenticated();
    }
}
